package com.mark.maven.ddd.app.translator;

import com.mark.maven.ddd.app.dto.common.AddressDTO;
import com.mark.maven.ddd.app.dto.common.ContactDTO;
import com.mark.maven.ddd.app.dto.common.CustomerDTO;
import com.mark.maven.ddd.app.dto.req.CreateOrderRequest;
import com.mark.maven.ddd.common.enums.OrderStatus;
import com.mark.maven.ddd.domain.CreateOrderDomainRequest;
import com.mark.maven.ddd.domain.vo.Address;
import com.mark.maven.ddd.domain.vo.Contact;
import com.mark.maven.ddd.domain.vo.Customer;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class CreateOrderTranslator {

    public CreateOrderDomainRequest toDomainRequest(CreateOrderRequest request) {
        return CreateOrderDomainRequest.builder()
                .orderStatus(OrderStatus.INIT.name())
                .createTime(new Date())
                .customer(toCustomer(request.getCustomer()))
                .sender(toSender(request.getSender()))
                .receiver(toReceiver(request.getReceiver()))
                .build();
    }

    private Customer toCustomer(CustomerDTO customer) {
        return Customer.builder()
                .customerNo(customer.getCustomerNo())
                .customerName(customer.getCustomerName())
                .build();
    }

    private Contact toSender(ContactDTO contact) {
        return Contact.builder()
                .name(contact.getName())
                .telephone(contact.getTelephone())
                .mobilePhone(contact.getMobilePhone())
                .address(toAddress(contact.getAddress()))
                .build();
    }

    private Contact toReceiver(ContactDTO contact) {
        return Contact.builder()
                .name(contact.getName())
                .telephone(contact.getTelephone())
                .mobilePhone(contact.getMobilePhone())
                .address(toAddress(contact.getAddress()))
                .build();
    }

    private Address toAddress(AddressDTO address) {
        return Address.builder()
                .provinceCode(address.getProvinceCode())
                .provinceName(address.getProvinceName())
                .cityCode(address.getCityCode())
                .cityName(address.getCityName())
                .districtCode(address.getDistrictCode())
                .districtName(address.getDistrictName())
                .townCode(address.getTownCode())
                .build();
    }
}
